/*
 *说明：用户与用户组关系表 已选用户列表脚本
 *
 *@author liuxun 2012-03-06
*/

var opeForm;
var grid;

$(function(){
	opeForm = $('#opeForm');

	grid=$('#listTable').datagrid({
		frozenColumns:[[
            {title:'选择',field:'ck',checkbox:true}
		]],
		columns:[[

            {title:'统一登录名',field:'LOGIN_NAME',align:'center',width:60},
            
            {title:'用户姓名',field:'USR_NAME',align:'center',width:80},
            
            {title:'用户类型',field:'TYPE_VALUE_NAME',align:'center',width:60},
            
            {title:'所属机构',field:'ORG_NAME',align:'center',width:150},
            
            {title:'负责人',field:'IS_LEADER',align:'center',width:40,
            	formatter:function(value, rec){
            		 return value == '1' ? "<font color='green'>√</font>" : '';
            	}
            },
            
            {title:'操作员',field:'IS_OPERATOR',align:'center',width:40,
            	formatter:function(value, rec){
            		 return value == '1' ? "<font color='green'>√</font>" : '';
            	}
            },
            
            {title:'用户状态',field:'STATUS_NAME',align:'center',width:40}
        ]],
		pagination:true,
		singleSelect: false
	});

	queryOperation();

	$('#queryBtn').click(clickQueryOperation);
	$('#clearBtn').click(clearOperation);
	$("#addBtn").click(addOperation);
	$("#deleteBtn").click(deleteOperation);

});

//翻页时自动调用此函数
function onLoadPrev(msg){
	var queryParams=grid.datagrid('options').queryParams;
	queryParams['firstQuery']=false;
}

//查询操作
function queryOperation(){
	var params = opeForm.serializeObject();
	grid.datagrid("options").pageNumber = 1;
	grid.datagrid({
		url: 'A_USR_USRGRP!querySelectedUsrList.do',
		queryParams: jQuery.extend({}, params, {'firstQuery': true})
	});
}

//点击查询操作，首次查询提示选择组信息
function clickQueryOperation(){
	var usr_grp_id = $('#usr_grp_id').val();
	if(usr_grp_id == '' || usr_grp_id == null){
		showInfoWin('请选择用户组！');
	}
	var params = opeForm.serializeObject();
	grid.datagrid("options").pageNumber = 1;
	grid.datagrid({
		url: 'A_USR_USRGRP!querySelectedUsrList.do',
		queryParams: jQuery.extend({}, params, {'firstQuery': true})
	});
}

//清空操作（重置）
function clearOperation() {
	opeForm.resetForm();
}

//查看明细
function showInfo(key) {
	var url = 'A_USR_USRGRP!showInfo.do?model.key=' + key;
	openWin(url,'pop'); 
}

//新增用户与用户组操作
function addOperation() {
	var selectedNode = parent.treeFrame.getSelecdedNode();
	
	if (!selectedNode) {
		showInfoWin('请先选中节点!');
		return;
	}
	
	//treeFrame.expandNode();
	
	var selectedNodeCode;
	if (selectedNode.id == '0') {
		showInfoWin('不能选择根节点!');
		return;
	} else {
		selectedNodeId = selectedNode.attributes.usr_grp_id;
	}
	
	var url = 'A_USR_USRGRP!showNotselectedUsr.do?model.usr.usr_status=01&model.usr_grp_id=' + selectedNodeId;
	openWin(url, 'pop');
}

// 删除用户与用户组操作
function deleteOperation() {
	var rows = grid.datagrid('getSelections');
	
	if (rows.length == 0) {
		showInfoWin('请您首先选中要删除的记录!');	
		return;
	}

	var ids = [];
	for (var i = 0; i < rows.length; i++) {
		ids.push(rows[i].AUTO_ID);
	}
	var params = $ .param({'id' : ids}, true);

	$ .messager.confirm('提示信息', '您确定要删除选定的' + rows.length + '条记录吗?', function(result){
		if(result){
			$ .ajax({
				url: 'A_USR_USRGRP!doDelete.do',
				data: params,
				type: 'post',
				success: function(data){
				    showInfoWin('操作成功!');
					afterOK();
				}
			});
		}	
	});
}

//选择机构
function select_a_org() {
	var action = "A_ORG!showTree.do?model.org_status=01";
	var row = showModalWin(action);
	
	if (row) {
		$('#org_name').val(row.attributes.org_name);
		$('#org_sequence').val(row.attributes.org_sequence);
	}
	
}

//操作成功后刷新列表
function afterOK(){
	queryOperation();
}